/*接收消息*/
function send(headSrc,str,time){
	var html="<div class='send'><div class=\"time\">"+ time +"</div><div class='msg'><img src="+headSrc+" />"+
	"<p><i class='msg_input'></i>"+str+"</p></div></div>";
	upView(html);
}
/*发送消息*/
function show(headSrc,str,time){
    var html="<div class='show'><div class=\"time\">"+ time +"</div><div class='msg'><img src="+headSrc+" />"+
        "<p><i class='msg_input'></i>"+str+"</p></div></div>";
    upView(html);
}
/*更新视图*/
function upView(html){
	$('.message').append(html);
	$('body').animate({scrollTop:$('.message').outerHeight()-window.innerHeight},200)
}

$(function(){
    //发送按钮的启用
	$('.footer').on('keyup','input',function(){
		if($(this).val().length>0){
			$(this).next().css('background','#114F8E').prop('disabled',true);
		
		}else{
			$(this).next().css('background','#ddd').prop('disabled',false);
        }
    });

    // 调用方法  循环请求信息
    var user = JSON.parse(sessionStorage.getItem('user'));  //用户
    var img = "";
    test();


    // 发送按钮的点击事件
    $('.footer p').click(function(){
        var dateNow = new Date();
        var str = $('.footer input').val();
        $.post('/messages',{'user.id':user.id,'target.id':crowdId,'content.content':str,type:2},function (data) {
            if(data.success){
                show(img,str,dateNow);
                $('.footer input').val('');
            };
        });
    });





    function test(){
        setInterval(function () {
            console.log(1);
            $.get('/messages/crowds/dialog',{userId:crowdId,targetId:user.id,type:2},function (data) {
                if (data){
                    console.log(data);
                    $.each(data,function (index, item) {
                        var headSrc = item.user.headImgUrl;
                        var str = item.content.content;
                        var time = item.creatdate;
                        img = item.target.headImgUrl;
                        send(headSrc,str,time);
                    })
                };

                $.ajax({
                    type: "PUT",
                    url: "/messages/batchMarkread",                     //应将状态改为拒绝
                    data: {"userId":crowdId,"targetId":user.id,type:2},
                    success: function(data){

                    }
                });
            })

        },1000);

        /*$(arr).each(function(i){
            setTimeout(function(){
                send("images/touxiang.png",arr[i])
            },500)
        })*/
    }
})


